<template>
	<view :style="{ height: conentHeight + 'px;' }">
		<view class="top-height">
			<view class="page-top-box">
				<view class="search-box flex ac">
					<u-search
						borderColor="#DADADA"
						bgColor="#ffffff"
						placeholder="搜索项目名称"
						v-model="searchForm.project_name"
						:showAction="false"
						@search="searchFunc"
						@clear="searchFunc"
					></u-search>
				</view>
			</view>
			<view class="search-tab-box flex ac">
				<view class="tab-width">
					<u-tabs
						@click="searchTabClick"
						:list="searchTabList"
						:current="searchForm.construction_status_index"
						lineHeight="0"
						inactiveStyle="width: 118rpx;height: 52rpx;background: #F8F8FA;border-radius: 26rpx;font-weight: 400;font-size: 26rpx;color: #919092;line-height:52rpx;text-align:center;"
						activeStyle="width: 118rpx;height: 52rpx;background: #3B75FF;border-radius: 26rpx;font-weight: 500;font-size: 26rpx;color: #FFFFFF;line-height:52rpx;text-align:center;"
					></u-tabs>
				</view>
				<view class="search-btn flex ac" @click="searchShow = true" :style="searchShow ? 'color:#3B75FF;' : ''">
					筛选
					<text class="iconfont icon-shaixuan" :style="searchShow ? 'color:#3B75FF;' : ''"></text>
				</view>
			</view>
		</view>
		<view class="list-content-box" :style="{ height: 'calc(100% - ' + topHeight + 'px);' }">
			<z-paging
				@onRefresh="refresh"
				ref="paging"
				:fixed="false"
				v-model="dataList"
				@query="queryList"
				:paging-style="{ 'background-color': '#f7f7f7', height: '100%', width: '100%' }"
			>
				<template slot="empty">
					<u-empty icon="https://hzybucket.obs.cn-south-1.myhuaweicloud.com/we_chat/no-msg2.png" text=" "></u-empty>
				</template>
				<view class="list-items flex ac flex-wrap" :key="index" v-for="(item, index) in dataList" @click.stop="navToDetailFunc(item)">
					<view class="list-items-title flex ac jb" v-if="$fieldShowFunc('sg_xmmc')">
						<view class="title u-line-1">{{ item.project_name }}</view>
					</view>
					<view class="list-items-item flex ac list-items-item-width50" v-if="$fieldShowFunc('sg_xmpp')">
						<view class="title">项目品牌</view>
						<view class="desc u-line-1">{{ item.brand_name }}</view>
					</view>
					<view class="list-items-item flex ac list-items-item-width50" v-if="$fieldShowFunc('sg_xmbh')">
						<view class="title">项目编号</view>
						<view class="desc u-line-1">{{ item.project_number }}</view>
					</view>
					<view class="list-items-item flex ac list-items-item-width50" v-if="$fieldShowFunc('sg_kclx')">
						<view class="title">勘场类型</view>
						<view class="desc u-line-1">
							<template v-if="item.survey_site == 1">无需勘场</template>
							<template v-if="item.survey_site == 2">实地勘场</template>
							<template v-if="item.survey_site == 3">勘场出图</template>
							<template v-if="!item.survey_site">--</template>
						</view>
					</view>
					<view class="list-items-item flex ac list-items-item-width50" v-if="$fieldShowFunc('sg_sgjd')">
						<view class="title">施工进度</view>
						<view class="desc u-line-1">
							<template v-if="item.construction_status == 30">待进场</template>
							<template v-if="item.construction_status == 35">待进场</template>
							<template v-if="item.construction_status == 40">待进场</template>
							<template v-if="item.construction_status == 42">待进场</template>
							<template v-if="item.construction_status == 45">施工中</template>
							<template v-if="item.construction_status == 50">已完工</template>
							<template v-if="item.construction_status == 60">质保中</template>
							<template v-if="item.construction_status == 70">质保到期</template>
							<template v-if="!item.construction_status">--</template>
						</view>
					</view>
					<view class="list-items-item flex ac">
						<view class="title">项目地址</view>
						<view class="desc u-line-1">{{ item.area + item.address }}</view>
					</view>
					<!-- 按钮 -->
					<view style="width: 100%">
						<judgeBtn plain :per_arr="per_arr" :btns="btns" objectType="construction" :state="item.construction_status" :item="item" @change="btnsChange"></judgeBtn>
					</view>
				</view>
			</z-paging>
		</view>
		<!-- 筛选弹窗 -->
		<u-popup :show="searchShow" round="30" @close="searchShow = false" closeable>
			<view class="search-popup-title">全部筛选</view>
			<scroll-view scroll-y class="search-popup-box">
				<view class="search-items">
					<view class="search-items-title flex ac jb">
						<text>项目类型</text>
						<view
							class="search-items-title-right flex ac jc"
							hover-class="hover-class"
							v-if="searchListObj.store_type_id.page != 1"
							@click="retractFunc('store_type_id')"
						>
							<text class="txt">收起</text>
							<u-icon name="arrow-up" color="#A1A7B3" size="12"></u-icon>
						</view>
					</view>
					<view class="search-items-list flex ac flex-wrap">
						<view
							@click="searchSelectFunc('store_type_id', item)"
							class="search-items-list-item"
							:class="item.id == searchForm.store_type_id ? 'search-items-list-item-active' : ''"
							hover-class="hover-class"
							v-for="(item, index) in searchListObj.store_type_id.list"
							:key="index"
						>
							{{ item.store_type_name }}
						</view>
						<view
							class="search-items-list-more flex ac jc"
							hover-class="hover-class"
							@click="lookMoreFunc('store_type_id')"
							v-if="searchListObj.store_type_id.list.length != searchListObj.store_type_id.max"
						>
							<text class="txt">查看更多</text>
							<u-icon name="arrow-down" color="#A1A7B3" size="12"></u-icon>
						</view>
					</view>
				</view>
				<view class="search-items">
					<view class="search-items-title flex ac jb">
						<text>施工工程经理</text>
						<view
							class="search-items-title-right flex ac jc"
							hover-class="hover-class"
							v-if="searchListObj.engineering_manager_id.page != 1"
							@click="retractFunc('engineering_manager_id')"
						>
							<text class="txt">收起</text>
							<u-icon name="arrow-up" color="#A1A7B3" size="12"></u-icon>
						</view>
					</view>
					<view class="search-items-list flex ac flex-wrap">
						<view
							@click="searchSelectFunc('engineering_manager_id', item)"
							:class="searchForm.engineering_manager_id == item.id ? 'search-items-list-item-active' : ''"
							class="search-items-list-item"
							hover-class="hover-class"
							v-for="(item, index) in searchListObj.engineering_manager_id.list"
							:key="index"
						>
							{{ item.name }}
						</view>
						<view
							class="search-items-list-more flex ac jc"
							hover-class="hover-class"
							@click="lookMoreFunc('engineering_manager_id')"
							v-if="searchListObj.engineering_manager_id.list.length != searchListObj.engineering_manager_id.max"
						>
							<text class="txt">查看更多</text>
							<u-icon name="arrow-down" color="#A1A7B3" size="12"></u-icon>
						</view>
					</view>
				</view>
				<view class="search-items">
					<view class="search-items-title flex ac jb">
						<text>工程监理</text>
						<view
							class="search-items-title-right flex ac jc"
							hover-class="hover-class"
							v-if="searchListObj.engineering_supervision_id.page != 1"
							@click="retractFunc('engineering_supervision_id')"
						>
							<text class="txt">收起</text>
							<u-icon name="arrow-up" color="#A1A7B3" size="12"></u-icon>
						</view>
					</view>
					<view class="search-items-list flex ac flex-wrap">
						<view
							@click="searchSelectFunc('engineering_supervision_id', item)"
							:class="searchForm.engineering_supervision_id == item.id ? 'search-items-list-item-active' : ''"
							class="search-items-list-item"
							hover-class="hover-class"
							v-for="(item, index) in searchListObj.engineering_supervision_id.list"
							:key="index"
						>
							{{ item.name }}
						</view>
						<view
							class="search-items-list-more flex ac jc"
							hover-class="hover-class"
							@click="lookMoreFunc('engineering_supervision_id')"
							v-if="searchListObj.engineering_supervision_id.list.length != searchListObj.engineering_supervision_id.max"
						>
							<text class="txt">查看更多</text>
							<u-icon name="arrow-down" color="#A1A7B3" size="12"></u-icon>
						</view>
					</view>
				</view>
				<view class="search-items">
					<view class="search-items-title">合同完工日期</view>
					<view @click="chooseDateFunc">
						<u--input readonly placeholder="合同完工日期" border="surround" v-model="searchForm.contract_completion_date_show"></u--input>
					</view>
				</view>
				<view class="search-items">
					<view class="search-items-title flex ac jb">
						<text>施工进度</text>
					</view>
					<view class="search-items-list flex ac flex-wrap">
						<view
							@click="searchSelectFunc('construction_status', item)"
							:class="searchForm.construction_status == item.value ? 'search-items-list-item-active' : ''"
							class="search-items-list-item"
							hover-class="hover-class"
							v-for="(item, index) in searchListObj.construction_status.list"
							:key="index"
						>
							{{ item.label }}
						</view>
					</view>
				</view>
				<view class="search-items">
					<view class="search-items-title flex ac jb">
						<text>风险标签</text>
					</view>
					<view class="search-items-list flex ac flex-wrap">
						<view
							@click="searchSelectFunc('risk_label', item)"
							:class="searchForm.risk_label == item.label ? 'search-items-list-item-active' : ''"
							class="search-items-list-item"
							hover-class="hover-class"
							v-for="(item, index) in searchListObj.risk_label.list"
							:key="index"
						>
							{{ item.label }}
						</view>
					</view>
				</view>
			</scroll-view>
			<view class="btn-box flex ac jb">
				<u-button
					@click="reSearchValFunc"
					text="重置"
					customStyle="width: 248rpx;height: 82rpx;background: #242834;border-radius: 12rpx;font-weight: 500;font-size: 30rpx;color: #FFFFFF;"
				></u-button>
				<u-button
					@click="searchFunc"
					text="查询"
					customStyle="width: 422rpx;height: 82rpx;background: #3B75FF;border-radius: 12rpx;font-weight: 500;font-size: 30rpx;color: #FFFFFF;"
				></u-button>
			</view>
		</u-popup>
		<!-- 创建时间筛选 -->
		<u-calendar
			:show="dateShow"
			mode="range"
			@confirm="dateConfirmFunc"
			@close="dateShow = false"
			:maxDate="maxDate"
			:minDate="minDate"
			:defaultDate="defaultDate"
			:monthNum="36"
		></u-calendar>
		<!-- 操作组件 -->
		<constructionOperate :value.sync="per_name" :oper_item="oper_item" @submit="handleSubmit"></constructionOperate>
	</view>
</template>

<script>
export default {
	name: 'constructionManagement',
	props: {
		conentHeight: {
			default: 0
		},
		option: {
			type: Object,
			default: {}
		}
	},
	provide() {
		return {
			providedData: this.btns
		};
	},
	data() {
		const d = new Date();
		const year = d.getFullYear();
		let month = d.getMonth() + 1;
		month = month < 10 ? `0${month}` : month;
		const date = d.getDate();
		return {
			defaultDate: [`${year}-${month}-${date}`],
			maxDate: `${year}-${month}-${date}`, //日历最大日期
			minDate: `${year - 2}-${month}-${date}`, //日历最小日期
			dateShow: false,
			searchShow: false,
			dataList: [],
			searchForm: {
				project_name: '',
				store_type_id: '',
				engineering_manager_id: '',
				engineering_supervision_id: '',
				contract_completion_date_show: '',
				contract_completion_date_start: '',
				contract_completion_date_end: '',
				construction_status: '',
				construction_status_index: 0,
				risk_label: ''
			},
			topHeight: 0,
			searchTabList: [
				{
					name: '全部',
					value: ''
				},
				{
					name: '待进场',
					value: 42
				},
				{
					name: '施工中',
					value: 45
				},
				{
					name: '已完工',
					value: 50
				}
			],
			searchListObj: {
				store_type_id: {
					list: [],
					page: 1,
					num: 5,
					max: 0
				},
				engineering_manager_id: {
					list: [],
					page: 1,
					num: 5,
					max: 0
				},
				engineering_supervision_id: {
					list: [],
					page: 1,
					num: 5,
					max: 0
				},
				construction_status: {
					list: [
						{
							label: '待进场',
							value: 42
						},
						{
							label: '施工中',
							value: 45
						},
						{
							label: '已完工',
							value: 50
						},
						{
							label: '质保中',
							value: 60
						},
						{
							label: '质保到期',
							value: 70
						}
					],
					page: 1,
					num: 5,
					max: 0
				},
				risk_label: {
					list: [
						{
							label: '一次预警'
						},
						{
							label: '二次预警'
						},
						{
							label: '已超期'
						},
						{
							label: '停工'
						}
					],
					page: 1,
					num: 5,
					max: 0
				}
			},
			per_arr: [], //权限列表
			// 操作按钮
			btns: [
				{
					per_name: 'projects_projectQualityRating',
					state: 50,
					name: '项目质量评分'
				},
				{
					per_name: 'edit_manager',
					func: (item) => {
						if (item.engineering_manager_name) {
							return true;
						} else {
							return false;
						}
					},
					name: '更换施工工程经理'
				},
				{
					per_name: 'add_manager',
					func: (item) => {
						if (!item.engineering_manager_name && item.construction_status == 35) {
							return true;
						} else {
							return false;
						}
					},
					name: '分配施工工程经理'
				},
				{
					per_name: 'construction_submit',
					func: (item) => {
						if (item.engineering_manager_name && item.construction_status == 42) {
							return true;
						} else {
							return false;
						}
					},
					name: '开工'
				},
				{
					per_name: 'refuse_orders',
					func: (item) => {
						if (item.engineering_manager_id == uni.getStorageSync('userInfo').id && item.construction_status == 42 && item.status < 1092) {
							return true;
						} else {
							return false;
						}
					},
					name: '拒绝接单'
				}
			],
			// 操作相关
			per_name: '',
			oper_item: {}
		};
	},
	mounted() {
		this.getStoreTypeList();
		this.getEngineeringManager();
		this.engineeringSupervisionList();
		this.getHeight();
		// 获取权限
		this.$Api
			.getHtmlAuthority({
				path: '/constructionManagement'
			})
			.then((res) => {
				console.log(res, 'getHtmlAuthority');
				if (res.code == 200) {
					this.per_arr = res.data.permission;
				}
			});
	},
	watch: {},
	methods: {
		// 刷新修改当前操作item
		refreshItemFunc() {
			console.log('刷新当前数据');
			let oper_item = this.oper_item;
			let index = this.dataList.findIndex((item) => {
				return item.id == oper_item.id;
			});
			if (index == -1) return;
			this.$Apipc
				.engineeringProjectDetails({
					project_id: oper_item.id
				})
				.then((res) => {
					console.log(res, '项目详情');
					if (res.code == 200) {
						this.dataList[index] = res.data;
						this.dataList.splice(0, 0);
					}
				});
		},
		// 操作反馈
		handleSubmit(e) {
			this.refreshItemFunc();
		},
		// 操作事件
		btnsChange(e) {
			console.log(e);
			this.per_name = e.per_name;
			this.oper_item = e.item;
		},
		refresh() {
			this.dataList = [];
		},
		// 重置弹窗筛选项
		reSearchValFunc() {
			this.searchForm = {
				project_name: '',
				store_type_id: '',
				engineering_manager_id: '',
				engineering_supervision_id: '',
				contract_completion_date_show: '',
				contract_completion_date_start: '',
				contract_completion_date_end: '',
				construction_status: '',
				construction_status_index: 0,
				risk_label: ''
			};
			this.$nextTick(() => {
				this.searchFunc();
			});
		},
		// 刷新列表
		searchFunc() {
			this.$refs.paging.reload();
			this.searchShow = false;
		},
		getHeight() {
			// 创建节点查询
			let that = this;
			const systemInfo = uni.getSystemInfoSync();
			const query = uni.createSelectorQuery().in(that);
			query.select('.top-height').boundingClientRect();
			query.exec((res) => {
				that.topHeight = res[0].height + systemInfo.statusBarHeight + 20;
			});
		},
		// 去详情
		navToDetailFunc(item) {
			this.oper_item = item;
			uni.navigateTo({
				url: '/pagesB/constructionManagementDetail/constructionManagementDetail?id=' + item.id
			});
		},
		queryList(page, num) {
			this.engineeringProjectList(page, num);
		},
		// 施工列表
		engineeringProjectList(page, num) {
			uni.showLoading({
				mask: true
			});
			let searchForm = this.searchForm;
			this.$Apipc
				.engineeringProjectList({
					page,
					num,
					project_name: searchForm.project_name,
					store_type_id: searchForm.store_type_id,
					engineering_manager_id: searchForm.engineering_manager_id,
					engineering_supervision_id: searchForm.engineering_supervision_id,
					contract_completion_date_start: searchForm.contract_completion_date_start,
					contract_completion_date_end: searchForm.contract_completion_date_end,
					construction_status: searchForm.construction_status,
					risk_label: searchForm.risk_label
				})
				.then((res) => {
					uni.hideLoading();
					console.log(res, '施工列表');
					if (res.code == 200) {
						this.$refs.paging.complete(res.data.list);
					}
				});
		},
		// 门店类型列表(项目类型)
		getStoreTypeList() {
			this.$Apipc
				.getStoreTypeList({
					page: this.searchListObj.store_type_id.page,
					num: this.searchListObj.store_type_id.num
				})
				.then((res) => {
					console.log(res, '门店类型列表');
					if (res.code == 200) {
						if (this.searchListObj.store_type_id.page == 1) {
							this.searchListObj.store_type_id.list = [];
						}
						this.searchListObj.store_type_id.list = [...this.searchListObj.store_type_id.list, ...res.data.list];
						this.searchListObj.store_type_id.max = res.data.max;
					}
				});
		},
		// 弹窗筛选项
		searchSelectFunc(type, item) {
			console.log(type, item);
			switch (type) {
				case 'store_type_id':
					this.searchForm[type] = this.searchForm[type] == item.id ? '' : item.id;
					break;
				case 'engineering_manager_id':
					this.searchForm[type] = this.searchForm[type] == item.id ? '' : item.id;
					break;
				case 'engineering_supervision_id':
					this.searchForm[type] = this.searchForm[type] == item.id ? '' : item.id;
					break;
				case 'construction_status':
					this.searchForm[type] = this.searchForm[type] == item.value ? '' : item.value;
					if (this.searchForm[type]) {
						let index = this.searchTabList.findIndex((child) => {
							return child.value == item.value;
						});
						this.searchForm.construction_status_index = index == -1 ? 0 : index;
					} else {
						this.searchForm.construction_status_index = 0;
					}
					break;
				case 'risk_label':
					this.searchForm[type] = this.searchForm[type] == item.label ? '' : item.label;
					break;
				default:
			}
		},
		// 弹窗筛选项查看更多
		lookMoreFunc(type) {
			switch (type) {
				case 'store_type_id':
					this.searchListObj.store_type_id.page += 1;
					this.getStoreTypeList();
					break;
				case 'engineering_manager_id':
					this.searchListObj.engineering_manager_id.page += 1;
					this.getEngineeringManager();
					break;
				case 'engineering_supervision_id':
					this.searchListObj.engineering_supervision_id.page += 1;
					this.engineeringSupervisionList();
					break;
				default:
			}
		},
		// 弹窗筛选项列表收起
		retractFunc(type) {
			switch (type) {
				case 'store_type_id':
					this.searchListObj.store_type_id.page = 1;
					this.getStoreTypeList();
					break;
				case 'engineering_manager_id':
					this.searchListObj.engineering_manager_id.page = 1;
					this.getEngineeringManager();
					break;
				case 'engineering_supervision_id':
					this.searchListObj.engineering_supervision_id.page = 1;
					this.engineeringSupervisionList();
					break;
				default:
			}
		},
		// 工程经理
		getEngineeringManager() {
			var obj = {
				page: this.searchListObj.engineering_manager_id.page,
				num: this.searchListObj.engineering_manager_id.num,
				keyword: '',
				province_id: '',
				city_id: '',
				county_id: '',
				identity: 2,
				position_id_arr: [7]
			};
			this.$Apipc
				.getAdminList(obj)
				.then((res) => {
					console.log(res, '工程经理');
					if (res.code == 200) {
						if (this.searchListObj.engineering_manager_id.page == 1) {
							this.searchListObj.engineering_manager_id.list = [];
						}
						this.searchListObj.engineering_manager_id.list = [...this.searchListObj.engineering_manager_id.list, ...res.data.list];
						this.searchListObj.engineering_manager_id.max = res.data.max;
					}
				})
				.catch((err) => {});
		},
		// 工程监理
		engineeringSupervisionList() {
			this.$Apipc
				.getAdminList({
					page: this.searchListObj.engineering_supervision_id.page,
					num: this.searchListObj.engineering_supervision_id.num,
					identity: 1,
					position_id_arr: [8]
				})
				.then((res) => {
					console.log(res, '工程监理列表');
					if (res.code == 200) {
						if (this.searchListObj.engineering_supervision_id.page == 1) {
							this.searchListObj.engineering_supervision_id.list = [];
						}
						this.searchListObj.engineering_supervision_id.list = [...this.searchListObj.engineering_supervision_id.list, ...res.data.list];
						this.searchListObj.engineering_supervision_id.max = res.data.max;
					}
				});
		},
		// 创建时间日历弹窗
		chooseDateFunc() {
			this.dateShow = true;
		},
		// 日期筛选确认
		dateConfirmFunc(e) {
			this.searchForm.contract_completion_date_start = e[0];
			this.searchForm.contract_completion_date_end = e[e.length - 1];
			this.searchForm.contract_completion_date_show = e[0] + '~' + e[e.length - 1];
			this.dateShow = false;
		},
		// 顶部tab快捷搜索
		searchTabClick(e) {
			this.searchForm.construction_status = e.value;
			this.searchForm.construction_status_index = e.index;
			this.$refs.paging.reload();
		}
	}
};
</script>

<style lang="scss"></style>
